package colab.persistencia;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import colab.dominio.Maquina;
import colab.dominio.Sala;

public class MaquinasDAO {
	
	//private ArrayList<Maquina> maquinas = new Maquinas();
	
	public ArrayList<Maquina> getMaquinas() throws SQLException{
		ArrayList<Maquina> maquinas = null;
		ResultSet rs = null;
		Connection conn = null;
		PreparedStatement stmt = null;
		String comando = "select * from notificacoes";
		try {
			maquinas = new ArrayList<Maquina>();
			conn = Conexao.getConnection();
			stmt = conn.prepareStatement(comando);
			rs = stmt.executeQuery();
			while(rs.next()){
				Maquina maquina = new Maquina();
				maquina.setId_maquina(rs.getInt("computador"));
				maquina.setEstado(rs.getString("status"));
				maquinas.add(maquina);
				}
			
	        
			}
		catch (SQLException e) {
			e.printStackTrace();
			}
		finally{
			stmt.close();
			conn.close();
			rs.close();			
			
		}
		return maquinas;
	}
	
	public ArrayList<Maquina> listarMaquinas(Sala sala) throws SQLException{
		ArrayList<Maquina> maquinas_lab = null;
		ResultSet rs = null;
		Connection conn = null;
		PreparedStatement stmt = null;
		String comando = "select * from computadores where lab like '"+sala.getId_sala()+"'";
		try {
			maquinas_lab = new ArrayList<Maquina>();
			conn = Conexao.getConnection();
			stmt = conn.prepareStatement(comando);
			rs = stmt.executeQuery();
			while(rs.next()){
				Maquina m = new Maquina();
				m.setId_maquina(rs.getInt("Id"));
				m.setCod_maquina(rs.getInt("codigo_maquina"));
				m.setCod_monitor(rs.getInt("codigo_monitor"));
				m.setSala(rs.getInt("lab"));
				maquinas_lab.add(m);
				}
	        
			}
		catch (SQLException e) {
			e.printStackTrace();
			}
		finally{
			stmt.close();
			conn.close();
			rs.close();
			
		}
		
		return maquinas_lab;
	}

	public Maquina getMaquina(int id){
		
		ResultSet rs = null;
		Connection conn = null;
		PreparedStatement stmt = null;
		Maquina maquina = new Maquina();
		String comando = "select * from computadores where Id ='"+id+"'";
		try {
			
			conn = Conexao.getConnection();
			stmt = conn.prepareStatement(comando);
			rs = stmt.executeQuery();
			if(rs.next()){
				
				maquina.setId_maquina(rs.getInt("Id"));
				maquina.setCod_maquina(rs.getInt("codigo_maquina"));
				maquina.setCod_monitor(rs.getInt("codigo_monitor"));
				maquina.setSala(rs.getInt("lab"));
				}
	        
			}
		catch (SQLException e) {
			e.printStackTrace();
			}
		finally{
			try {
				stmt.close();
				conn.close();
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
		}
		
		return maquina;
	}
	
	
	
}

